package com.ct.musicserver.service.impl;

import com.ct.musicserver.common.errorcode.ServiceErrorCodeConstants;
import com.ct.musicserver.common.exception.ServiceException;
import com.ct.musicserver.common.utils.Constant;
import com.ct.musicserver.common.utils.JacksonUtil;
import com.ct.musicserver.controller.result.UserLoginResult;
import com.ct.musicserver.dao.dataobject.LoveMusicDO;
import com.ct.musicserver.dao.dataobject.MusicDO;
import com.ct.musicserver.dao.mapper.LoveMusicMapper;
import com.ct.musicserver.dao.mapper.MusicMapper;
import com.ct.musicserver.service.LoveMusicService;
import com.ct.musicserver.service.dto.FindMusicDTO;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class LoveMusicServiceImpl implements LoveMusicService {

    @Autowired
    private HttpSession session;
    @Autowired
    private LoveMusicMapper loveMusicMapper;
    @Autowired
    private MusicMapper musicMapper;

    @Override
    public void likeMusic(Long id) {
        if (null == id) {
            throw new ServiceException(ServiceErrorCodeConstants.MUSIC_IS_NOT_CHOICE);
        }
        UserLoginResult userInfo = (UserLoginResult) session.getAttribute(Constant.USERINFO_SESSION_KEY);
        Long userid = userInfo.getId();
        LoveMusicDO loveMusicDO = loveMusicMapper.selectByMusicIdAndUserId(id, userid);
        if (null != loveMusicDO) {
            throw new ServiceException(ServiceErrorCodeConstants.MUSIC_IS_LIKE);
        }
        loveMusicDO = new LoveMusicDO();
        loveMusicDO.setMusicId(id);
        loveMusicDO.setUserId(userid);
        loveMusicMapper.insert(loveMusicDO);
    }

    @Override
    public List<FindMusicDTO> findLoveMusic(String musicName) {
        UserLoginResult userInfo = (UserLoginResult) session.getAttribute(Constant.USERINFO_SESSION_KEY);
        Long userid = userInfo.getId();
        List<LoveMusicDO> loveMusicDOList = loveMusicMapper.selectByUserId(userid);
        System.out.println(JacksonUtil.writerValueAsString(loveMusicDOList));

        List<MusicDO> musicDOList = loveMusicDOList.stream()
                .map(loveMusicDO -> {
                    Long id = loveMusicDO.getMusicId();
                    MusicDO musicDO = musicMapper.selectByMusicId(id);
                    return musicDO;
                }).collect(Collectors.toList());

        List<FindMusicDTO> findMusicDTOList = musicDOList.stream()
                .map(musicDO -> {
                    FindMusicDTO findMusicDTO = new FindMusicDTO();
                    findMusicDTO.setId(musicDO.getId());
                    findMusicDTO.setSinger(musicDO.getSinger());
                    findMusicDTO.setTime(musicDO.getTime());
                    findMusicDTO.setTitle(musicDO.getTitle());
                    findMusicDTO.setUrl(musicDO.getUrl());
                    findMusicDTO.setUserid(musicDO.getUserid());
                    return findMusicDTO;
                }).collect(Collectors.toList());
        return findMusicDTOList;
    }

    @Override
    public void deletelovemusic(Long id) {
        if (null == id) {
            throw new ServiceException(ServiceErrorCodeConstants.MUSIC_IS_NOT_CHOICE);
        }
        UserLoginResult userInfo = (UserLoginResult) session.getAttribute(Constant.USERINFO_SESSION_KEY);
        Long userid = userInfo.getId();
        loveMusicMapper.deleteByMusicIdAndUserId(id, userid);
    }
}
